<template>
  <ekDialog
    v-model="ekVisiable"
    :title="title"
    @submit="handleSubmit"
    :hideBtn="disabled"
  >
    <div class="ek-form" v-loading="loading">
      <el-form
        :model="form"
        :rules="rules"
        label-width="160px"
        ref="forms"
        :disabled="disabled"
        v-if="!loading"
      >
      
        <el-form-item prop="user_nick" label="公告标题">
          <ekInput
            v-model="form.notice_title"
          />
        </el-form-item>
        <el-form-item prop="user_nick" label="公告内容" v-if="ekVisiable">
          <ekEditor 
            v-model="form.notice_content" 
        />
        </el-form-item>
         <el-form-item prop="notice_show" label="自动弹出">
            <ekRadio
              v-model="form.notice_show"
              :options="stateOptions"
            />
            </el-form-item>   

           
      </el-form>
    </div>
  </ekDialog>
</template>

<script setup>
import {
  watch,
  getCurrentInstance,
  computed,
  reactive,
  ref,
  defineEmits,
  onMounted 
} from "vue";
import { apis } from "../data.js";
import ekDialog from "@/components/business/ekDialog/index.vue";
import ekInput from "@/components/form/ekInput/index.vue";
import ekSelect from "@/components/form/ekSelect/index.vue";
import ekTableSelect from "@/components/form/ekTableSelect/index.vue";
import ekSelectModel from "@/components/form/ekSelectModel/index.vue";
import ekEditor from "@/components/form/ekEditor/index.vue";
import ekRadio from "@/components/form/ekRadio/index.vue";

import { getDimensionByCode } from "@/utils/sys.js";
import { objectAssignment } from "@/utils/common.js";
const props = defineProps({
  // 显示隐藏
  visiable: {
    type: Boolean,
    default: false,
  },
  rowId: {
    type: [String, Number],
    default: "",
  },
  title: {
    type: String,
    default: "",
  },
  disabled: {
    type: Boolean,
    default: false,
  },
});
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();
const { $storage, $http, $ekLoading, $ekMessage } = globalProperties;

const emit = defineEmits(["update:visiable", "submit"]);

// 是否显示
const ekVisiable = computed({
  get() {
    return props.visiable;
  },
  set(value) {
    emit("update:visiable", value);
  },
});

function handleSubmit(done) {
  let url = props.rowId ? apis.edit : apis.add;
  forms.value.validate((valid) => {
    if (valid) {
      $http
        .request({
          url: url,
          method: props.rowId ? "PUT" : "POST",
          data: {
            id:props.rowId || '',
            ...form.value,
          },
        })
        .then((res) => {
          $ekMessage.success(res.message);
          emit("submit", done);
        })
        .catch((err) => {});
    }
  });
}
const loading = ref(false);
let forms = ref(null);
let form = ref({
  notice_title: "",
  notice_content: "",
  notice_show: "1"
});
const initForm = () => {
  form.value = {
    notice_title: "",
    notice_content: "",
    notice_show: "1"
  };
};
let rules = reactive({
  
});

// 初始化表单
async function init() {
  loading.value = true;
  // 详情或者编辑
  if (props.rowId) {
    $http
      .get(apis.detail, {
        params: {
          id: props.rowId
        }
        
      })
      .then((res) => {
        if (Object.keys(res.data).length) {
         
          objectAssignment(form.value, res.data);
        }
        loading.value = false;
      });
  } else {
    loading.value = false;
  }
}
watch(
  () => props.visiable,
  (val) => {
    if (val) {
      initForm();
      init();
    }
  },

);
const stateOptions = ref([]);
onMounted(async () => {
  stateOptions.value = await getDimensionByCode("DIM_TYPE_DEFAULT");
});
</script>

<style scoped lang="scss"></style>
